package itheima_edu_info_manager.dao;


import itheima_edu_info_manager.domain.Student;

import java.util.ArrayList;

public class OtherStudentDao implements BaseStudentDao{
    //1、创建Student学生数组长度为5
    private static ArrayList<Student> studentArray = new ArrayList<>();

    static {
        //模拟读取上次存储的数据
        studentArray.add(new Student("heima001","余凯琦","22","1998-10-28"));
        studentArray.add(new Student("heima002","许启航","23","1997-07-14"));
    }

    //此方法用于添加学生
    public boolean addStudent(Student stu) {
        studentArray.add(stu);
        return true;
    }
    //查看学生
    public Student[] findAllStudent() {
        //findAllStudent方法将学生数组返回即可
        Student[] stus = new Student[studentArray.size()];
        for (int i = 0; i < studentArray.size(); i++) {
            stus[i] = studentArray.get(i);
        }

        return stus;
    }
    //删除学生
    public boolean deleteStudentById(String id) {
        /*
            1、遍历数组查找该id，该方法能被调用说明学生数组中一定是有学生对象的
            2、该索引位置的学生对象，使用null经行替换
        */
        for (int i = 0; i < studentArray.size(); i++) {
            if (studentArray.get(i).getId().equals(id)) {
                studentArray.remove(i);
                return true;
            }
        }
        return false;
    }
    //修改学生
    public boolean updateStudent(Student newStudent, String id) {
        for (int i = 0; i < studentArray.size(); i++) {
            if (studentArray.get(i).getId().equals(id)) {
                studentArray.set(i,newStudent);
                return true;
            }
        }
        return false;
    }
}
